package com.ccys.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.app.service.ReceiptService;
import com.ccys.common.third.mailGun.MailgunUtil;
import com.ccys.common.util.HtmlUtils;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.dto.app.receipt.SendDocumentDto;
import com.ccys.db.entity.TblMember;
import com.ccys.db.mapper.TblBookingImageMapper;
import com.ccys.db.mapper.TblBookingMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: WhiteCoat
 * @description: 收据接口
 * @author: ZhangJingCan
 * @create: 2024-02-07 13:51
 **/

@Service
public class ReceiptServiceImpl implements ReceiptService {

    @Resource
    private TblBookingMapper tblBookingMapper;

    @Resource
    private TblBookingImageMapper tblBookingImageMapper;

    @Override
    public ResponseResultApp sendDocument(SendDocumentDto dto) throws Exception {
        //基础校验
        if (ObjectUtil.isEmpty(dto.getDocument_type()) || ObjectUtil.isEmpty(dto.getEmails()) || ObjectUtil.isEmpty(dto.getBooking_id())) {
            return ResponseResultApp.error("Parameter abnormality");
        }
        //获取当前booking的member
        TblMember memberByBooking = tblBookingMapper.findMemberByBookingId(dto.getBooking_id());
        if (ObjectUtil.isEmpty(memberByBooking)) {
            return ResponseResultApp.error("data error");
        }
        //单个发送
        if (4 == dto.getDocument_type()) {
            //得到pdf文件名称
            String fileName = tblBookingImageMapper.findOneFileNameByBookingIdAndByType(dto.getBooking_id(), 10);
            //https://whcapi-qa.whitecoat.global/v1/download/
            StringBuffer buffer = new StringBuffer();
            buffer.append("https://whcapi-qa.whitecoat.global/v1/download/")
                    .append(memberByBooking.getId()).append("/").append(dto.getBooking_id())
                    .append("/").append(dto.getDocument_type());
            Map<String, String> map = new HashMap<>();
            map.put(fileName, buffer.toString());
            //获取收件的邮箱地址 - 并且使用,拼接
            java.util.List<String> emails = dto.getEmails();
            String mailsStr = emails.isEmpty() ? "" : (emails.size() == 1 ? emails.get(0) : String.join(", ", emails));


            //主题：WhiteCoat Medical Document(s):
            StringBuffer memberNameByBookingId = new StringBuffer();
            memberNameByBookingId.append("WhiteCoat Medical Document(s):").append(memberByBooking.getFirstName())
                    .append(memberByBooking.getLastName() != null ? memberByBooking.getLastName() : "");

            //获取邮件模版
//            Map<String,Object> data = new HashMap<>();
            //todo 注意 - 这里是直接扔出去了一个邮件的模版 - 并未处理 - 现在先这样 - 目前不知道怎么做
            String templateContent = HtmlUtils.getTemplateContent("SendDocument.ftl", null);
            //发送邮件
            MailgunUtil.sendEmail(mailsStr, memberNameByBookingId.toString(), null, templateContent, map);
        } else {
            //发送所有

        }

        return null;
    }

}
